package com.nbicc.hot.service.impl;

import com.nbicc.common.MyValues;
import com.nbicc.common.RequestPageObj;
import com.nbicc.hot.service.INewsVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Set;

/**
 * @Author: lilingyan
 */
@Service("NewsVisitServiceImpl")
public class NewsVisitServiceImpl implements INewsVisitService {

    /**
     * 返回给前端的访问计数字段
     */
    public final static String VISIT_COUNT = "visitCount";

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void incrementVisit(Number newId) {
        redisTemplate.opsForZSet().incrementScore(MyValues.VISIT_COUNT,newId,1);
    }

    @Override
    public void deleteVisitCount(Number newId) {
        redisTemplate.opsForZSet().remove(MyValues.VISIT_COUNT,newId);
    }

    @Override
    public long getVisitCount(Number newId) {
        Double score = redisTemplate.opsForZSet().score(MyValues.VISIT_COUNT, newId);
        if(score == null){
            return 0;
        }
        return score.longValue();
    }

    /**
     * 获取最热门的新闻id
     * @param requestPageObj
     * @return
     */
    @Override
    public Set getVisitCountIds(RequestPageObj requestPageObj) {
        Set newIdSet = redisTemplate.opsForZSet().reverseRange(MyValues.VISIT_COUNT , 0 , requestPageObj.getPageSize());
        return newIdSet;
    }

}
